//
//  HomeHeaderView.m
//  intelligent
//
//  Created by Ace Portgas on 2024/2/18.
//

#import "HomeHeaderView.h"
#import "HomeCell.h"
#import "HomeNewCell.h"
#import "HeaderImageView.h"
#import "SuggestModel.h"
@interface HomeHeaderView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,assign) NSInteger currentIndex;
@property (nonatomic,strong) NSMutableArray * dataSource;
@end
@implementation HomeHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _currentIndex = 1;
        self.tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.bounces = NO;
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        
        [self.tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:@"HomeCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"HomeNewCell" bundle:nil] forCellReuseIdentifier:@"HomeNewCell"];
        HeaderImageView * headerView = [[HeaderImageView alloc]init];
        headerView.frame = CGRectMake(0, 0, SCREEN_WIDETH, 215);
        self.tableView.tableHeaderView = headerView;
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.tableView);
            make.size.mas_offset(CGSizeMake(SCREEN_WIDETH, 215));
        }];
        
        UIView * fotterView = [UIView new];
        fotterView.frame = CGRectMake(0, 0, SCREEN_WIDETH, 30);
        self.tableView.tableFooterView = fotterView;
        UIButton * refresh = [UIButton buttonWithType:UIButtonTypeCustom];
        refresh.frame = CGRectMake(SCREEN_WIDETH - 20  - 90, 0, 90, 30);
        refresh.titleLabel.font = [UIFont systemFontOfSize:14];
        [refresh setTitle:SQNSLocalString(@"刷新问题") forState:UIControlStateNormal];
        [refresh setImage:SQGetImage(@"刷新") forState:UIControlStateNormal];
        [refresh setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        // 设置图片和文字的间距
        refresh.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 8);
        refresh.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
        // 设置内容的对齐方式，让图片在右边，文字在左边
        refresh.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        refresh.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [refresh addTarget:self action:@selector(clickRefreshQuestionBtn) forControlEvents:UIControlEventTouchUpInside];
        [fotterView addSubview:refresh];
        
        [self loadData];
    }
    return self;
}

-(void)clickRefreshQuestionBtn{
    _currentIndex++;
    [self loadData];
}

-(void)loadData{
    NSDictionary * dic = @{
        @"current":[NSNumber numberWithInteger:_currentIndex % 2 + 1],
        @"size":@"4"
    };
    [HttpTool post:SYWT paramDic:[NSMutableDictionary dictionaryWithDictionary:dic] successBlock:^(BaseModel * _Nonnull httpModel) {
        NSLog(@"%@",httpModel);
        [self.dataSource removeAllObjects];
        self.dataSource = [SuggestModel mj_objectArrayWithKeyValuesArray:httpModel.data[@"records"]];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.tableView reloadData];
}

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
////    return self.dataSource.count;
//    return 4;
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeNewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeNewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[HomeNewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeNewCell"];
    }
    SuggestModel * model = self.dataSource[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    cell.clickSendBlock = ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(sendQuestion:)]) {
            [self.delegate sendQuestion:model.name];
        }
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SuggestModel * model = self.dataSource[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickQuestion:)]) {
        [self.delegate clickQuestion:model.name];
    }
}

@end
